func numSubmatrixSumTarget(matrix [][]int, target int) (ans int) {
	for i := range matrix { //枚举上边界
		sum := make([]int, len(matrix[0]))
		for _, row := range matrix[i:] { //枚举下边界
			for j := range matrix[0] { //算出每一列的和
				sum[j] += row[j] //sum为每一列的前缀和
			}
			ans += subarraySum(sum, target)
		}
	}

	return ans
}

func subarraySum(nums []int, k int) (ans int) {
	m := make(map[int]int)
	pre := 0
	m[0] = 1 //处理左开头边界
	ans = 0
	for i := 0; i < len(nums); i++ {
		pre += nums[i]
		if v, ok := m[pre-k]; ok {
			ans += v //要加v而不是只加1
		}
		m[pre]++
	}
	return ans
}
